package com.bee.beemanage.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bee.beemanage.api.service.SysRoleService;
import com.bee.beemanage.api.entity.SysRole;
import com.bee.beemanage.api.entity.SysRoleMenu;
import com.bee.beemanage.api.entity.SysRolePermission;
import com.bee.beemanage.api.entity.SysUser;
import com.bee.beemanage.api.mapper.SysRoleMapper;
import com.bee.beemanage.api.mapper.SysRoleMenuMapper;
import com.bee.beemanage.api.mapper.SysRolePermissionMapper;
import com.bee.beemanage.api.mapper.SysUserMapper;
import com.bee.beemanage.system.common.utils.result.Result;
import com.bee.beemanage.system.common.utils.result.ResultCode;
import com.bee.beemanage.system.common.utils.result.ResultTool;
import com.bee.beemanage.system.common.utils.transaction.TransUtils;
import com.bee.beemanage.system.common.utils.transaction.Transaction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 陈国松
 * @since 2022-10-09
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;
    
    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public Page<SysRole> recycleSearch(Page<SysRole> page, SysRole sysRole, LocalDateTime startTime, LocalDateTime endTime) {
        return sysRoleMapper.recycleSearch(page,sysRole,startTime,endTime);
    }

    /**
     * 批量物理删除
     *
     * @return 删除是否成功
     */
    @Override
    public Result clear() {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            trans = TransUtils.getTrans();
            trans.begin();
            //查询逻辑删除的角色
            QueryWrapper<SysRole> queryMenu = Wrappers.query();
            queryMenu.eq("is_deleted","1");
            List<SysRole> sysRoles = sysRoleMapper.selectList(queryMenu);
            List<String> roleIds = new ArrayList<>();
            sysRoles.forEach(sysRole ->roleIds.add(sysRole.getId()));
            if(!roleIds.isEmpty()){
                QueryWrapper<SysRoleMenu> query = Wrappers.query();
                query.in("role_Id",roleIds);
                sysRoleMenuMapper.delete(query);
            }
            sysRoleMapper.clear();
            trans.commit();
        } catch (Exception e) {
            if(null!=trans){
                trans.rollback();
            }
            result = ResultTool.fail();
            result.setErrorMsg(e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 批量恢复逻辑删除的数据
     *
     * @param ids 要恢复的id列表
     * @return 恢复是否成功
     */
    @Override
    public boolean recover(List<String> ids) {
        return sysRoleMapper.recover(ids);
    }

    @Override
    public Result safeDeleteById(String id) {
        Result result = ResultTool.success();
        Transaction trans = null;
        try {
            trans = TransUtils.getTrans();
            trans.begin();
            //判断是否有用户使用到了此角色
            QueryWrapper<SysUser> query = Wrappers.query();
            query.eq("is_deleted", "0")
                    .eq("role_id", id);
            List<SysUser> sysUsers = sysUserMapper.selectList(query);
            if (null != sysUsers && sysUsers.size() > 0) {
                result = ResultTool.fail(ResultCode.IN_USING);
                result.setErrorMsg("角色被使用,无法删除");
                return result;
            }
            //删除对应的角色权限
            QueryWrapper<SysRolePermission> queryRP = Wrappers.query();
            queryRP.eq("role_id", id);
            sysRolePermissionMapper.delete(queryRP);
            //删除对应的角色菜单
            QueryWrapper<SysRoleMenu> queryRM = Wrappers.query();
            queryRM.eq("role_id", id);
            sysRoleMenuMapper.delete(queryRM);
            //删除角色
            sysRoleMapper.deleteById(id);
            trans.commit();
        }catch (Exception e) {
            if(null!=trans){
                trans.rollback();
            }
            result = ResultTool.fail();
            result.setErrorMsg(e.getMessage());
            return result;
        }
        return result;
    }
}
